package com.henu.yizhiya.db;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.henu.yizhiya.entity.Room;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by y on 2017/5/17.
 */
//数据库管理类
public class DBManager {
    private static DBManager manager;
    RoomDbHelper dbHelper;
    SQLiteDatabase readDb;
    SQLiteDatabase writeDb;

    public static DBManager getInstance(Context context){
        if (manager==null){
            manager = new DBManager(context);
        }
        return manager;
    }

    private DBManager(Context context){
        dbHelper = new RoomDbHelper(context);
        readDb = dbHelper.getReadableDatabase();
        writeDb = dbHelper.getWritableDatabase();
    }

    public void insert(Room room,String num){

        String sql="insert into room values('"+room.getR_num()+"','"+room.getWeek_1()+"','"
                +room.getWeek_2()+"','"+room.getWeek_3()+"','"+room.getWeek_4()+"','"
                +room.getWeek_5()+"','"+room.getWeek_6()+"','"+room.getWeek_7()+"','"+num+"')";

        writeDb.execSQL(sql);
    }



    public ArrayList<RoomDB> search(String num){
        String key = whichDay();
        ArrayList<RoomDB> list = new ArrayList<>();
        Cursor cursor =readDb.rawQuery("select "+key+",r_num from room where "+"build_num"+"=='"+num+"'",null);
        while(cursor.moveToNext()){
            RoomDB room= new RoomDB();
            String result = cursor.getString(cursor.getColumnIndex(key));
            String r_num = cursor.getString(cursor.getColumnIndex("r_num"));
            room.setP1(result.charAt(0)+"");
            room.setP2(result.charAt(1)+"");
            room.setP3(result.charAt(2)+"");
            room.setP4(result.charAt(3)+"");
            room.setP5(result.charAt(4)+"");
            room.setP6(result.charAt(5)+"");
            room.setR_num(r_num);
            list.add(room);
        }
        return list;
    }

    public ArrayList<RoomDB> searchAll(){
        String key = whichDay();
        ArrayList<RoomDB> list = new ArrayList<>();
        Cursor cursor =readDb.rawQuery("select "+key+",r_num from room",null);
        while(cursor.moveToNext()){
            RoomDB room= new RoomDB();
            String result = cursor.getString(cursor.getColumnIndex(key));
            String r_num = cursor.getString(cursor.getColumnIndex("r_num"));
            room.setP1(result.charAt(0)+"");
            room.setP2(result.charAt(1)+"");
            room.setP3(result.charAt(2)+"");
            room.setP4(result.charAt(3)+"");
            room.setP5(result.charAt(4)+"");
            room.setP6(result.charAt(5)+"");
            room.setR_num(r_num);
            list.add(room);
        }
        return list;
    }

    public String whichDay(){
        String key="week_";
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        int whichDay = calendar.get(Calendar.DAY_OF_WEEK)-1;
        if (whichDay==0){
            key=key+7;
        }else {
            key =key+whichDay;
        }
        return key;
    }
}
